/*
* Copyright (C) 2012-2018 Doubango Telecom <http://www.doubango.org>
* License: BSD
* This file is part of Open Source sipML5 solution <http://www.sipml5.org>
*/
import {tsdp_message} from './tsdp_message'
import {tsdp_header_A} from './headers/tsdp_header_A'



// var tsdp_path = DispatchLibPath+"tinySDP/"
//
// function tsdp_api_add_js_scripts(s_elt) {
//     var tag_hdr = document.getElementsByTagName(s_elt)[0];
//     for (var i = 1; i < arguments.length; ++i) {
//         var tag_script = document.createElement('script');
//         tag_script.setAttribute('type', 'text/javascript');
//         tag_script.setAttribute('src', arguments[i]);
//         tag_hdr.appendChild(tag_script);
//     }
// };
//
// // add tinySAK API
// tsdp_api_add_js_scripts('head', tsdp_path + 'tsk/tsk_api.js');
//
// tsdp_api_add_js_scripts('head',
//     tsdp_path + 'headers/tsdp_header.js',
//     // tsdp_path + 'headers/tsdp_header_A.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_B.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_C.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_Dummy.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_E.js', #see<tsdp_header_Str.js>
//     // tsdp_path + 'headers/tsdp_header_I.js', #see<tsdp_header_Str.js>
//     // tsdp_path + 'headers/tsdp_header_K.js', #see<tsdp_header_Str.js>
//     // tsdp_path + 'headers/tsdp_header_M.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_O.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_P.js', #see<tsdp_header_Str.js>
//     // tsdp_path + 'headers/tsdp_header_R.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_S.js', #see<tsdp_header_Str.js>
//     // tsdp_path + 'headers/tsdp_header_Str.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_T.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_U.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_V.js', #include_in<tsdp_header.js>
//     // tsdp_path + 'headers/tsdp_header_Z.js' #include_in<tsdp_header.js>
//
//     tsdp_path + 'tsdp_message.js'
//     //'src/tinySDP/src/tsdp_parser_message.js' #include_in<tsdp_message.js>
// );


var changesdp = function(peersdp, videocandidates, audiocandidates){
	var sdp = peersdp;

	var tsdp = tsdp_message.prototype.Parse(sdp);

    for (var i = 0; i < tsdp.ao_headers.length; ++i){
        if (tsdp.ao_headers[i] &&
            tsdp.ao_headers[i].e_type.c_name == "s")
        {
            tsdp.ao_headers[i].s_value = "webrtc";
            break;
        }
    }

	var opus_pt = 0;

	var m_audio = tsdp.get_header_m_by_name("audio");
    if (m_audio)
    {
        for (var i = 0; i < m_audio.ao_hdr_A.length; ++i){
            if (m_audio.ao_hdr_A[i] &&
                m_audio.ao_hdr_A[i].s_field == "rtpmap")
            {
                var substr_array = m_audio.ao_hdr_A[i].s_value.split(" ");
                if (substr_array &&
                    substr_array[1] &&
                    substr_array[1].indexOf("opus") == 0)
                {
                    opus_pt = substr_array[0];
                    break;
                }
            }
        }

        console.log('opus_pt ' + opus_pt);
        m_audio.as_fmt = new Array();
        m_audio.as_fmt[0] = opus_pt.toString();

        for (var i = 0; i < m_audio.ao_hdr_A.length; ++i){
            if (m_audio.ao_hdr_A[i] &&
                (m_audio.ao_hdr_A[i].s_field == "rtpmap" ||
                m_audio.ao_hdr_A[i].s_field == "fmtp" ||
                m_audio.ao_hdr_A[i].s_field == "rtcp-fb" ||
                m_audio.ao_hdr_A[i].s_field == "extmap"))
            {
                var substr_array = m_audio.ao_hdr_A[i].s_value.split(" ");
                if (!(substr_array &&
                    substr_array[0] == opus_pt))
                {
                    m_audio.ao_hdr_A[i] = null;
                }
            }
        }

        //m_audio.s_proto = "RTP/AVP";

        for (var i = 0; i < audiocandidates.length; ++i){
            var a = tsdp_header_A.prototype.Parse(audiocandidates[i]);
            m_audio.ao_hdr_A.push(a);
        }
    }

	//video
	var h264_pt_array = new Array;
	var h264_pt = 0;

	var m_video = tsdp.get_header_m_by_name("video");
    if (m_video)
    {
        for (var i = 0; i < m_video.ao_hdr_A.length; ++i){
        	if (m_video.ao_hdr_A[i] &&
        		m_video.ao_hdr_A[i].s_field == "rtpmap")
        	{
        		var substr_array = m_video.ao_hdr_A[i].s_value.split(" ");
        		if (substr_array &&
        			substr_array[1] &&
        			substr_array[1].indexOf("H264") == 0)
        		{
        			h264_pt_array.push(substr_array[0]);
        		}
        	}
        }
        //"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f"
        for (var i = 0; i < m_video.ao_hdr_A.length; ++i){
        	if (m_video.ao_hdr_A[i] &&
        		m_video.ao_hdr_A[i].s_field == "fmtp")
        	{
        		var substr_array = m_video.ao_hdr_A[i].s_value.split(" ");
        		if (substr_array && substr_array[0] && substr_array[1])
        		{
        			for (var j = 0; j < h264_pt_array.length; ++j)
                    {
                        if (substr_array[0] == h264_pt_array[j] &&
                            substr_array[1].indexOf("packetization-mode=1") != -1 &&
                            substr_array[1].indexOf("profile-level-id=42e0") != -1)
                        {
                            //优先Baseline
                            h264_pt = substr_array[0];
                            break;
                        }
                    }

        		}
        	}

            if (h264_pt)
            {
                break;
            }
        }

        if (h264_pt == 0)
        {
            h264_pt = h264_pt_array[0];
        }

        console.log('h264_pt ' + h264_pt);
        m_video.as_fmt = new Array();
        m_video.as_fmt[0] = h264_pt.toString();

        for (var i = 0; i < m_video.ao_hdr_A.length; ++i){
        	if (m_video.ao_hdr_A[i] &&
        		(m_video.ao_hdr_A[i].s_field == "rtpmap" ||
        		m_video.ao_hdr_A[i].s_field == "fmtp" ||
        		m_video.ao_hdr_A[i].s_field == "rtcp-fb" ||
        		m_video.ao_hdr_A[i].s_field == "extmap"))
        	{
        		var substr_array = m_video.ao_hdr_A[i].s_value.split(" ");
        		if (!(substr_array &&
        			substr_array[0] == h264_pt))
        		{
        			m_video.ao_hdr_A[i] = null;
        		}
        	}
        }

        //m_video.s_proto = "RTP/AVP";

        for (var i = 0; i < videocandidates.length; ++i){
        	var a = tsdp_header_A.prototype.Parse(videocandidates[i]);
        	m_video.ao_hdr_A.push(a);
        }
    }

	return tsdp.toString();
}

var changesdp_local = function(localsdp){
	var sdp = localsdp;

	var tsdp = tsdp_message.prototype.Parse(sdp);

    for (var i = 0; i < tsdp.ao_headers.length; ++i){
        if (tsdp.ao_headers[i] &&
            tsdp.ao_headers[i].e_type.c_name == "s")
        {
            tsdp.ao_headers[i].s_value = "webrtc";
            break;
        }
    }

	var opus_pt = 0;

	var m_audio = tsdp.get_header_m_by_name("audio");
    if (m_audio)
    {
        for (var i = 0; i < m_audio.ao_hdr_A.length; ++i){
        	if (m_audio.ao_hdr_A[i] &&
        		m_audio.ao_hdr_A[i].s_field == "rtpmap")
        	{
        		var substr_array = m_audio.ao_hdr_A[i].s_value.split(" ");
        		if (substr_array &&
        			substr_array[1] &&
        			substr_array[1].indexOf("opus") == 0)
        		{
        			opus_pt = substr_array[0];
        			break;
        		}
        	}
        }

        console.log('opus_pt ' + opus_pt);
        m_audio.as_fmt = new Array();
        m_audio.as_fmt[0] = opus_pt.toString();

        for (var i = 0; i < m_audio.ao_hdr_A.length; ++i){
        	if (m_audio.ao_hdr_A[i] &&
        		(m_audio.ao_hdr_A[i].s_field == "rtpmap" ||
        		m_audio.ao_hdr_A[i].s_field == "fmtp" ||
        		m_audio.ao_hdr_A[i].s_field == "rtcp-fb" ||
        		m_audio.ao_hdr_A[i].s_field == "extmap"))
        	{
        		var substr_array = m_audio.ao_hdr_A[i].s_value.split(" ");
        		if (!(substr_array &&
        			substr_array[0] == opus_pt))
        		{
        			m_audio.ao_hdr_A[i] = null;
        		}
        	}
        }
    }

	//video
	var h264_pt_array = new Array;
	var h264_pt = 0;

	var m_video = tsdp.get_header_m_by_name("video");
    if (m_video)
    {
        for (var i = 0; i < m_video.ao_hdr_A.length; ++i){
        	if (m_video.ao_hdr_A[i] &&
        		m_video.ao_hdr_A[i].s_field == "rtpmap")
        	{
        		var substr_array = m_video.ao_hdr_A[i].s_value.split(" ");
        		if (substr_array &&
        			substr_array[1] &&
        			substr_array[1].indexOf("H264") == 0)
        		{
        			h264_pt_array.push(substr_array[0]);
        		}
        	}
        }
        //"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f"
        for (var i = 0; i < m_video.ao_hdr_A.length; ++i){
        	if (m_video.ao_hdr_A[i] &&
        		m_video.ao_hdr_A[i].s_field == "fmtp")
        	{
        		var substr_array = m_video.ao_hdr_A[i].s_value.split(" ");
        		if (substr_array && substr_array[0] && substr_array[1])
        		{
        			for (var j = 0; j < h264_pt_array.length; ++j)
                    {
                        if (substr_array[0] == h264_pt_array[j] &&
                            substr_array[1].indexOf("packetization-mode=1") != -1 &&
                            substr_array[1].indexOf("profile-level-id=42e0") != -1)
                        {
                            //优先Baseline
                            h264_pt = substr_array[0];
                            break;
                        }
                    }

        		}
        	}

            if (h264_pt)
            {
                break;
            }
        }

        if (h264_pt == 0)
        {
            h264_pt = h264_pt_array[0];
        }

        console.log('h264_pt ' + h264_pt);
        m_video.as_fmt = new Array();
        m_video.as_fmt[0] = h264_pt.toString();

        for (var i = 0; i < m_video.ao_hdr_A.length; ++i){
        	if (m_video.ao_hdr_A[i] &&
        		(m_video.ao_hdr_A[i].s_field == "rtpmap" ||
        		m_video.ao_hdr_A[i].s_field == "fmtp" ||
        		m_video.ao_hdr_A[i].s_field == "rtcp-fb" ||
        		m_video.ao_hdr_A[i].s_field == "extmap"))
        	{
        		var substr_array = m_video.ao_hdr_A[i].s_value.split(" ");
        		if (!(substr_array &&
        			substr_array[0] == h264_pt))
        		{
        			m_video.ao_hdr_A[i] = null;
        		}
        	}
        }

    }

	return tsdp.toString();
}
// module.exports = {
//   changesdp: changesdp,
//   changesdp_local: changesdp_local,
// };
export {changesdp}
export {changesdp_local}
